/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.hibernate.proteu.util;




//import br.com.tminterface.facesbeans.shared.ArrayItems;
//import br.com.tminterface.utils.Cpf;
//import br.com.tminterface.utils.ZerosFabrica;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;






/**
 *
 * @author eros & ricardo mano    
 */
public class FireBirdProteuHelper {



      static protected Logger log = Logger.getLogger("logs4.tmlab");
//  static public    List<MaterialBean>  listMaterialBeans = new ArrayList<MaterialBean>();




       /**
        *
        *
        * @return a new Session if session is null or a session if session is not null
        */




    public static Session getSessionDude(){

        Session session = null;
        if(session == null){
            return session = HibernateUtilProteuFaces.getSession();
        }else{
            return session;
        }
    }


    /**
     *
     * @param clazz
     * @param id
     * @return Object
     */
     public static Object getObject(Class clazz,long id){

        Session session = getSessionDude();//sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Object obj = null;
        try{
        obj  =   session.get(clazz, id);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }



      /**
     *
     * @param clazz
     * @param id
     * @return Object
     */
     public static Object getObject(Class clazz,String id){

        Session session = getSessionDude();//sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Object obj = null;
        try{
        obj  =   session.get(clazz, id);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }


     /**
     *
     * @param clazz
     * @param id
     * @return Object
     */
     public static Object getObject(Class clazz,Object idPk){

        Session session = getSessionDude();//sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Object obj = null;
        try{
        obj  =   session.get(clazz,(Serializable) idPk);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

     
     /**
     *
     * @param Object
     * @param id
     * @return Object
     */
     public static boolean deleteObject(Object obj){

        Session session ;
        Transaction tx ;

                if(obj != null){

                    session =getSessionDude();//sessionFactory.getCurrentSession();
                    tx = session.beginTransaction();
                            try{

                                session.delete(obj);
                                tx.commit();
                                
                            return true;
                            }finally{
                                if(session != null){
                                    session.close();
                                }
                            }
                }
        
                    return false;

    }

       /**
     *
     * @param Object
     * @param id
     * @return Object
     */
     public static boolean deleteListObject(List listObj){

        Session session ;
        Transaction tx ;

                if(listObj != null){

                    session =getSessionDude();//sessionFactory.getCurrentSession();
                    tx = session.beginTransaction();
                            try{

                                for(Object o : listObj){
                                    session.delete(o);
                                }

                                tx.commit();

                            return true;
                            }finally{
                                if(session != null){
                                    session.close();
                                }
                            }
                }

                    return false;

    }



      /**
     *
     * @param clazz
     * @param id
     * @return List<Object>
     */
     public static List  getListObject(Class clazz){

        Session session = getSessionDude();
        Transaction tx = session.beginTransaction();
        List result ;


        try{
        Criteria criteria = session.createCriteria(clazz);
         result = criteria.list();
        tx.commit();
        return result;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

      /**
     *
     * @param clazz
     * @param id
     * @return List<Object>
     */
     public static List  getListObjectOrderByField(Class clazz,String orderByField,boolean orderBy){

        Session session = getSessionDude();
        Transaction tx = session.beginTransaction();
        List result ;


        try{
        Criteria criteria = session.createCriteria(clazz);
                    if(orderBy){
                        criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }

         result = criteria.list();
        tx.commit();
        return result;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }







      /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKey( Class classBean, String strKey, Object value){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.like(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }

  /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKeyLike( Class classBean, String strKey, Object value){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.like(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }



  /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKeyEq( Class classBean, String strKey, Object value){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.eq(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }



  /**
 *
 * @param classBean
 * @param strKey
 * @param value
   * @param orderByField
   * @param ascOrDesc
 * @return a first Object from a List of obejcts
 */
 static  public List getListObjectByKeyEqOrderBy( Class classBean, String strKey, Object value,String orderByField,boolean  ascOrDesc){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.eq(strKey, value));
            }

                    if(ascOrDesc){
                        criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }

            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result;
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }


      /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKey( Class classBean, String strKey, Object value,boolean orderBy){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.like(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }

            if(orderBy){
                 criteria.addOrder( Order.asc(strKey) );
            }else{
                criteria.addOrder( Order.desc(strKey) );
            }

        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }



      /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a List of obejcts
 */
 static  public java.util.List getListOfObjectByKey( Class classBean, String strKey, Object value){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
           

            if(strKey != null){
                 session = getSessionDude();//sessionFactory.getCurrentSession();
                 tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);
                criteria.add(Restrictions.like(strKey, value));
                result = criteria.list();
            }


            
            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }


  static  public java.util.List getListOfObjectByKeyEq( Class classBean, String strKey, Object value){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{
            
            if(strKey != null){
                session = getSessionDude();//sessionFactory.getCurrentSession();
                tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);

                criteria.add(Restrictions.eq(strKey, value));
                criteria.add(Restrictions.isNotNull(strKey));
                result = criteria.list();
            }
            

            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }


    static  public java.util.List getListOfObjectByKeyEq( Class classBean, String strKey, Object value,String orderByField,boolean orderBy){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{

            if(strKey != null){
                session = getSessionDude();//sessionFactory.getCurrentSession();
                tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);

                criteria.add(Restrictions.eq(strKey, value));
                criteria.add(Restrictions.isNotNull(strKey));
                    if(orderBy){
                     criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }
                result = criteria.list();
            }


            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }


     static  public java.util.List getListOfObjectOrderByField( Class classBean,String orderByField,boolean orderBy){

        Session session = null;
        Transaction tx = null;
        List result = null;
        try{

          
                session = getSessionDude();//sessionFactory.getCurrentSession();
                tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);

                //criteria.add(Restrictions.eq(strKey, value));
                //criteria.add(Restrictions.isNotNull(strKey));
                    if(orderBy){
                     criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }
                result = criteria.list();
            


            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }



    /**
     *
     * @param obj save a obj
     *
     */
     public static void saveObject(Object obj){
        Session session = null;
        Transaction tx = null;
        try{
        session = getSessionDude();

        tx = session.beginTransaction();
        session.save(obj);
        tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


    }
    /**
     *
     * @param save a list of objects
     */
     public static void saveObjectList(List list){

         Session session = null;
         Transaction tx = null;
         try{
             session = getSessionDude();
             tx = session.beginTransaction();

             for(Object obj: list){
                 session.save(obj);
             }
             tx.commit();
         }finally{
             if(session != null){
                 session.close();
             }
         }

    }

     /**
     *
     * @param obj save or update a obj
     *
     */
     public static void saveOrUpdateObject(Object obj){
        Session session = null;
        Transaction tx = null;
        try{
        session = getSessionDude();
        //session = getSessionDude();
        tx = session.beginTransaction();
        session.saveOrUpdate(obj);
        tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


    }
     /**
     *
     * @param save or update a list of objects
     */
     public static void saveOrUpdateObjectList(List list){

         Session session = null;
         Transaction tx = null;
         try{
             session = getSessionDude();
             tx = session.beginTransaction();

             for(Object obj: list){
                 session.saveOrUpdate(obj);
             }
             tx.commit();
         }finally{
             if(session != null){
                 session.close();
             }
         }

    }

     /**
     *
     * @param update a obj
     *
     */
     public static void updateObject(Object obj){
        Session session = null;
        Transaction tx = null;
        try{
        session = getSessionDude();
        //session = getSessionDude();
        tx = session.beginTransaction();
        session.update(obj);
        tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

        /**
     *
     * @param update a obj and return the same
     *
     */
     public static Object  updateObjectReturnObject(Object obj){
        Session session = null;
        Transaction tx = null;

        try{
        session = getSessionDude();
        //session = getSessionDude();
        tx = session.beginTransaction();

        session.update(obj);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

     /**
     *
     * @param save a list of objects
     */
     public static void updateObjectList(List list){

         Session session = null;
         Transaction tx = null;
         try{
             session = getSessionDude();
             tx = session.beginTransaction();

             for(Object obj: list){
                 session.update(obj);
             }
             tx.commit();
         }finally{
             if(session != null){
                 session.close();
             }
         }

    }

   



}





